<?php

class mobileapi_ctl_admin_indexad_group extends desktop_controller{
    /**
     * 构造方法
     * @params object app object
     * @return null
     */
    public function __construct($app)
    {
        parent::__construct($app);
        $this->app_ad_id = array(
            1 => array(
                'value' => 'app广告轮播图'
                ),
	    2 => array(
                'value' => 'app文字广告'
                ),
        );
    }
    var $workground = 'mobileapi.wrokground.mobileapi';
    function index(){
        $this->finder('mobileapi_mdl_indexad_group',array(
            'title'=>app::get('b2c')->_('广告分组'),
            'actions'=>array(
                array('label'=>app::get('b2c')->_('添加'),'icon'=>'add.gif','href'=>'index.php?app=mobileapi&ctl=admin_indexad_group&act=create','target'=>'_blank'),

            )
            ));
    }

    function create(){
    	$group = array(
    			"group_code" => "group_",
    			"disabled" => "false",
    			'begin_time' => time(),
    			'end_time' => time(),
    			"ordernum" => "50",
    	);
    	$this->pagedata['group'] = $group;
        $app_ad = $this->app_ad_id;
        foreach($this->app_ad_id as $k=>$ad){
            $ad = kernel::database()->select(sprintf("select * from sdb_mobileapi_indexad_group where app_ad_key = ".$k." and showtype = 'app_ad' "));

            if(count($ad) > 0){
                $app_ad[$k]['is_checked'] = 1;
            }else{
                $app_ad[$k]['is_checked'] = 0;
            }
        }
    	$this->pagedata['app_ad_id'] = $app_ad;
        
    	$this->singlepage('admin/indexad/group_detail.html');
    }

    function save(){
    	$this->begin('');
    	$objAd = $this->app->model('indexad_group');
    	
    	$data = $_POST;
    	
    	$hour = $param['_DTIME_']['H'];
    	$begin_h = $hour['begin_time'];
    	$end_h = $hour['end_time'];
    	
    	// 开始时间&结束时间
    	foreach ($data['_DTIME_'] as $val) {
    		$temp['begin_time'][] = $val['begin_time'];
    		$temp['end_time'][] = $val['end_time'];
    	}
    	$data['begin_time'] = strtotime($data['begin_time'].' '. implode(':', $temp['begin_time']));
    	$data['end_time'] = strtotime($data['end_time'].' '. implode(':', $temp['end_time']));
    	
    	unset($data['_DTYPE_TIME']);
    	unset($data['_DTIME_']);
        if($data['showtype'] !== 'app_ad'){
            $data['app_ad_key'] = 0;
        }
    	
    	if ($objAd->save($data)) {
    		$this->end(true,app::get('b2c')->_('保存成功'));
    	} else {
    		$this->end(true,app::get('b2c')->_('保存失败'));
    	}
    	
    }
   

    function edit($group_id){
    	header("Cache-Control:no-store");
    	
        $this->path[] = array('text'=>app::get('b2c')->_('广告分组编辑'));
        $group_obj = $this->app->model('indexad_group');
        $this->pagedata['group'] = $group_obj->dump($group_id);
        $app_ad = $this->app_ad_id;
        foreach($this->app_ad_id as $k=>$ad){
            $ad = kernel::database()->select(sprintf("select * from sdb_mobileapi_indexad_group where app_ad_key = ".$k." and showtype = 'app_ad' and group_id <> ".$group_id));

            if(count($ad) > 0){
                $app_ad[$k]['is_checked'] = 1;
            }else{
                $app_ad[$k]['is_checked'] = 0;
            }
        }
    	$this->pagedata['app_ad_id'] = $app_ad;

        $this->singlepage('admin/indexad/group_detail.html');
    }

}
